package com.stt.design.iterator;

import com.stt.design.model.DesignTask;

import java.util.Iterator;
import java.util.List;

/**
 * 迭代器模式 - 优先级任务迭代器
 */
public class PriorityTaskIterator implements Iterator<DesignTask> {

    private List<DesignTask> tasks; // 待迭代的任务列表
    private int currentIndex; // 当前迭代到的索引
    private int minPriority; // 最小优先级

    /**
     * 构造函数
     * @param tasks 待迭代的任务列表
     * @param minPriority 最小优先级
     */
    public PriorityTaskIterator(List<DesignTask> tasks, int minPriority) {
        this.tasks = tasks;
        this.minPriority = minPriority;
    }

    /**
     * 是否有下一个元素
     * @return true/false
     */
    @Override
    public boolean hasNext() {
        while (currentIndex < tasks.size()) {
            if (tasks.get(currentIndex).getPriority() >= minPriority) {
                return true;
            }
            currentIndex++;
        }
        return false;
    }
    /**
     * 迭代到下一个元素
     * @return 迭代到的元素
     */
    @Override
    public DesignTask next() {
        return tasks.get(currentIndex++);
    }
}
